import { isArray } from 'lodash-es';

/**
 * 构造树型结构数据
 * @param data 数据源
 * @param labelField label字段
 * @param valueField value字段
 *
 */
export function handleTree(data: any[], labelField = 'label', valueField = 'value', children = 'children') {
  if (!isArray(data)) {
    console.warn('数据不是数组结构');
    return [];
  }
  const three = data.map((one: any) => {
    one.label = one[labelField];
    one.value = one[valueField];
    if (one[children] && one[children].length) {
      one.children = handleTree(one[children], labelField, valueField, children);
    }
    return one;
  });

  return three;
}
